import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import Components from 'unplugin-vue-components/vite';
import { VantResolver } from 'unplugin-vue-components/resolvers';
import DefineOptions from 'unplugin-vue-define-options/vite';
import { visualizer } from 'rollup-plugin-visualizer'
import legacy from '@vitejs/plugin-legacy';

const isVisualizer = process.env.VISUALIZER === 'show'
export default defineConfig({
  //base: '/',  //H5打包用
  base: './', // APP打包用
  plugins: [
    vue(),
    Components({
      resolvers: [VantResolver()],
    }),
    DefineOptions(),
    isVisualizer && visualizer({ gzipSize: true }),
    legacy({
      targets: ['defaults', 'not IE 11'],
    }),
  ],
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@use '@/assets/css/variable.scss' as *;`
        
      },
    }
  },
  server: {
    open: true,
    port: 3333,
    hmr: true,
    host: '0.0.0.0',
    proxy: {
      "/api": {
        target: "https://47.90.209.163:8086",
        // target: "http://198.13.39.109:8081",
        changeOrigin: true,
        // rewrite: (path) => path.replace(/^\/wap/, ""),
        secure: false
      },
    },
  },
  resolve: {
    dedupe: [
      'vue'
    ],
    alias: {
      'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js',
      "~": path.resolve(__dirname, './'),
      "@": path.resolve(__dirname, 'src'),
    }
  },
  build: {
    assetsDir: "static",
    rollupOptions: {
      input: {
        index: path.resolve(__dirname, "index.html"),
      },
      output: {
        chunkFileNames: 'js/[name]-[hash].js',
        entryFileNames: "js/[name]-[hash].js",
        assetFileNames: "[ext]/name-[hash].[ext]"
      },
    },
  },
  externals:['vue']
})

const globalSass = config => {
  const oneOfsMap = config.module.rule('scss').oneOfs.store
  oneOfsMap.forEach(item => {
    item
      .use('sass-resources-loader')
      .loader('sass-resources-loader')
      .options({
        resources: '@/assets/css/init.scss'
      })
      .end()
  });
}
